﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;

namespace Kxzz.Controls
{
    public class myDataObject : IDataObject
    {
        private const uint E_FAIL = 0x80004005;
        private const uint E_NOTIMPL = 0x80004001;
        private const uint E_POINTER = 0x80004003;
        private Bitmap mBitmap = new Bitmap(0x10, 0x10);
        public FORMATETC mpFormatetc = new FORMATETC();
        private const uint S_OK = 0;

        public uint DAdvise(ref FORMATETC a, int advf, IAdviseSink pAdvSink, out uint pdwConnection)
        {
            Trace.WriteLine("DAdvise");
            pdwConnection = 0;
            return 0x80004001;
        }

        public uint DUnadvise(uint dwConnection)
        {
            Trace.WriteLine("DUnadvise");
            return 0x80004001;
        }

        public uint EnumDAdvise(out IEnumSTATDATA ppenumAdvise)
        {
            Trace.WriteLine("EnumDAdvise");
            ppenumAdvise = null;
            return 0x80004001;
        }

        public uint EnumFormatEtc(uint dwDirection, IEnumFORMATETC penum)
        {
            Trace.WriteLine("EnumFormatEtc");
            return 0;
        }

        public uint GetCanonicalFormatEtc(ref FORMATETC pFormatetcIn, out FORMATETC pFormatetcOut)
        {
            Trace.WriteLine("GetCanonicalFormatEtc");
            pFormatetcOut = new FORMATETC();
            return 0x80004001;
        }

        public uint GetData(ref FORMATETC pFormatetc, ref STGMEDIUM pMedium)
        {
            IntPtr hDst = this.mBitmap.GetHbitmap();
            pMedium.tymed = 0x10;
            pMedium.unionmember = hDst;
            pMedium.pUnkForRelease = IntPtr.Zero;
            return 0;
        }

        public uint GetDataHere(ref FORMATETC pFormatetc, out STGMEDIUM pMedium)
        {
            Trace.WriteLine("GetDataHere");
            pMedium = new STGMEDIUM();
            return 0x80004001;
        }

        public uint QueryGetData(ref FORMATETC pFormatetc)
        {
            Trace.WriteLine("QueryGetData");
            return 0x80004001;
        }

        public uint SetData(ref FORMATETC a, ref STGMEDIUM b, bool fRelease)
        {
            Trace.WriteLine("SetData");
            return 0;
        }

        public void SetImage(Image image)
        {
            try
            {
                this.mBitmap = new Bitmap(image);
                this.mpFormatetc.cfFormat = CLIPFORMAT.CF_BITMAP;
                this.mpFormatetc.ptd = IntPtr.Zero;
                this.mpFormatetc.dwAspect = DVASPECT.DVASPECT_CONTENT;
                this.mpFormatetc.lindex = -1;
                this.mpFormatetc.tymed = TYMED.TYMED_GDI;
            }
            catch
            {
            }
        }

        public void SetImage(string strFilename)
        {
            try
            {
                this.mBitmap = (Bitmap) Image.FromFile(strFilename, true);
                this.mpFormatetc.cfFormat = CLIPFORMAT.CF_BITMAP;
                this.mpFormatetc.ptd = IntPtr.Zero;
                this.mpFormatetc.dwAspect = DVASPECT.DVASPECT_CONTENT;
                this.mpFormatetc.lindex = -1;
                this.mpFormatetc.tymed = TYMED.TYMED_GDI;
            }
            catch
            {
            }
        }
    }
}

